package cn.org.xwb.bookmall.user.utils;

import cn.org.xwb.bookmall.model.properties.MinioProp;
import cn.org.xwb.common.file.dto.FileSliceUploadDto;
import cn.org.xwb.common.file.utils.FileUtil;
import cn.org.xwb.common.file.vo.FileSliceUploadVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class Convert2DtoUtil {

    @Autowired
    private MinioProp minioProp;

    /**
     * 根据 FileSliceUploadVo 生成 FileSliceUploadDto
     *
     * @param fileSliceUploadVo
     * @return
     */
    public FileSliceUploadDto genFileSliceUploadDto(FileSliceUploadVo fileSliceUploadVo) {
        FileSliceUploadDto fileSliceUploadDto = new FileSliceUploadDto();
        fileSliceUploadDto.setFilename(fileSliceUploadVo.getFilename());
        fileSliceUploadDto.setFileUploadTime(fileSliceUploadVo.getFileUploadTime());
        fileSliceUploadDto.setFileSize(fileSliceUploadVo.getFileSize());
        fileSliceUploadDto.setFileType(fileSliceUploadVo.getFileType());
        fileSliceUploadDto.setMd5(fileSliceUploadVo.getMd5());
        fileSliceUploadDto.setChunkTotal(fileSliceUploadVo.getChunkTotal());
        fileSliceUploadDto.setChunkIdx(fileSliceUploadVo.getChunkIdx());
        fileSliceUploadDto.setChunk(fileSliceUploadVo.getChunk());
        fileSliceUploadDto.setBucket(minioProp.getBucket());
        fileSliceUploadDto.setDirName(fileSliceUploadVo.getMd5() + "/" + minioProp.getTempDir());
        fileSliceUploadDto.setObjName(fileSliceUploadVo.getMd5() + "/" + minioProp.getTempDir() + "/" + fileSliceUploadVo.getChunkIdx());
        fileSliceUploadDto.setExtension(FileUtil.getExtensionByFilename(fileSliceUploadVo.getFilename()));
        if (Objects.isNull(fileSliceUploadDto.getExtension()) || fileSliceUploadDto.getExtension().isEmpty()) {
            fileSliceUploadDto.setExtension(FileUtil.getExtensionByMimeType(fileSliceUploadVo.getFileType()));
        }
        fileSliceUploadDto.setMimeType(FileUtil.getMimeType(fileSliceUploadDto.getExtension()));
        fileSliceUploadDto.setMinioFilename(fileSliceUploadVo.getMd5() + "/" + fileSliceUploadVo.getMd5() + fileSliceUploadDto.getExtension());
        return fileSliceUploadDto;
    }

}
